import { removeEmptyValues } from "@/utils/common";
import { agentConfType, agentOptionsType, agentServerTreeType, agentSettingType, ResultCommon, ResultCommonList, ResultPagination } from "@/utils/commonType";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";
import exp from "constants";

/** 获取代理配置项清单 */
export const getAgentOptionList = async (data?) => {

    const result = await http.request<ResultPagination<agentOptionsType>>(
        "get",
        baseUrlApi("/agent/list"),
        {
            params: removeEmptyValues(data)
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
};

/** 删除代理配置项 */
export const removeAgentOption = async (aoId: string) => {
    const result = await http.request<ResultCommon<number>>(
        "delete",
        baseUrlApi(`/agent/remove/${aoId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
};

/** 保存代理配置项 */
export const saveAgentOption = async (data: agentOptionsType) => {
    const result = await http.request<ResultCommon<number>>(
        "post",
        baseUrlApi(`/agent/save`),
        {
            data: data
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
};

/** 获取代理配置数据清单 */
export const getAgentSettingList = async (section: string, data?: any) => {

    const result = await http.request<ResultPagination<agentSettingType>>(
        "get",
        baseUrlApi(`/agentSetting/list/${section}`),
        {
            params: removeEmptyValues(data)
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
};

/** 保存代理配置项 */
export const saveAgentSetting = async (data: agentSettingType) => {
    const result = await http.request<ResultCommon<number>>(
        "post",
        baseUrlApi(`/agentSetting/save`),
        {
            data: data
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
};

export const getAgentServerTreeData = async (roleType: number, tenantId?: string) => {
    const param = {
        roleType: roleType,
        tenantId: tenantId
    }
    const result = await http.request<ResultCommonList<agentServerTreeType>>(
        "get",
        baseUrlApi(`/agentSetting/serverTreeDate`),
        {
            params: removeEmptyValues(param)
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const getAgentServerTreeData2 = async (roleType: number, tenantId?: string) => {
    const param = {
        roleType: roleType,
        tenantId: tenantId
    }
    const result = await http.request<ResultCommonList<agentServerTreeType>>(
        "get",
        baseUrlApi(`/agentSetting/serverTreeDate2`),
        {
            params: removeEmptyValues(param)
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const getAgentConfList = async (param: any) => {
    const result = await http.request<ResultPagination<agentConfType>>(
        "get",
        baseUrlApi(`/agentContext/list`),
        {
            params: removeEmptyValues(param)
        }
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const getAgentServerStatus = async () => {
    const result = await http.request<ResultCommon<boolean>>(
        "get",
        baseUrlApi(`/agentContext/checkRunStatus`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const restartAgentServer = async () => {
    const result = await http.request<ResultCommon<boolean>>(
        "get",
        baseUrlApi(`/agentContext/reloadServer`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const createGlobalAgentConfContext = async () => {
    const result = await http.request<ResultCommon<string>>(
        "post",
        baseUrlApi(`/agentContext/createGlobalConf`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const createServerAgentConfContext = async (serverId: string) => {
    const result = await http.request<ResultCommon<string>>(
        "post",
        baseUrlApi(`/agentContext/createServerConf/${serverId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const createAppAgentConfContext = async (appId: string) => {
    const result = await http.request<ResultCommon<string>>(
        "post",
        baseUrlApi(`/agentContext/createLocationConf/${appId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const validateAgentConfig = async (confId: string) => {
    const result = await http.request<ResultCommon<boolean>>(
        "get",
        baseUrlApi(`/agentContext/validateConf/${confId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const publishAgentConfig = async (confId: string) => {
    const result = await http.request<ResultCommon<boolean>>(
        "post",
        baseUrlApi(`/agentContext/publishAgentConf/${confId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const callbackAgentConfig = async (confId: string) => {
    const result = await http.request<ResultCommon<boolean>>(
        "post",
        baseUrlApi(`/agentContext/callBackAgentConf/${confId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}

export const delAgentConfig = async (confId: string) => {
    const result = await http.request<ResultCommon<boolean>>(
        "delete",
        baseUrlApi(`/agentContext/delAgentConf/${confId}`)
    );
    if (result.code != 200) {
        throw new Error(result.message);
    }
    return result;
}